<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * @param {number[]} skills
       * @param {number} k
       * @return {number}
       */
      var findWinningPlayer = function (skills, k) {
        let w = 0; // 赢的次数
        let index = 0; // 当前下标
        for (let i = 1; i < skills.length; i++) {
          if (skills[index] > skills[i]) {
            w++;
          } else {
            w = 1;
            index = i;
          }

          if (w === k) {
            return index;
          }
        }
        return index; // 如果一轮遍历后没赢到k次 此时游戏高手肯定是最大的那个 迟早他会赢到k次的
      };

      console.log("玩家" + findWinningPlayer([4, 2, 6, 3, 9], 2) + "获胜");
    </script>
  </body>
</html>
